package leetcode;

import java.util.HashMap;
import java.util.Map;

public class l0290_单词规律 {

    public static boolean wordPattern(String pattern, String s) {
        //1 前置校验
        if (pattern == null || pattern.isEmpty() || s == null || s.isEmpty()) {
            return false;
        }
        String[] strArr = s.split(" ");
        if (pattern.length() != strArr.length) {
            return false;
        }
        //2 业务逻辑
        Map<Character, String> dataMap = new HashMap<>();
        for (int i = 0; i < pattern.length(); i++) {
            char key = pattern.charAt(i);
            if (dataMap.get(key) == null) {
                dataMap.put(key, strArr[i]);
                continue;
            }
            if (!dataMap.get(key).equals(strArr[i])) {
                return false;
            }
        }
        Map<String, Character> dataMapPlus = new HashMap<>();
        for (int i = 0; i < pattern.length(); i++) {
            String key = strArr[i];
            if (dataMapPlus.get(key) == null) {
                dataMapPlus.put(key, pattern.charAt(i));
                continue;
            }
            if (dataMapPlus.get(key) != pattern.charAt(i)) {
                return false;
            }
        }
        return true;
    }
}
